package cn.admobiletop.adsuyi.adapter.toutiao.listener;

import android.os.Handler;
import android.os.Looper;

import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTFullScreenVideoAd;

import cn.admobiletop.adsuyi.ad.listener.ADSuyiFullScreenVodAdListener;
import cn.admobiletop.adsuyi.adapter.toutiao.data.FullScreenVodAdInfo;
import cn.admobiletop.adsuyi.config.ADSuyiErrorConfig;

/**
 * @author ciba
 * @description 全屏视频广告监听
 * @date 2020/4/2
 */
public class FullScreenVodAdListener extends BaseAdListener<ADSuyiFullScreenVodAdListener> implements TTAdNative.FullScreenVideoAdListener {
    private FullScreenVodAdInfo fullScreenVodAdInfo;
    private Handler handler = new Handler(Looper.getMainLooper());

    public FullScreenVodAdListener(String platformPosId, ADSuyiFullScreenVodAdListener adListener) {
        super(platformPosId, adListener);
    }

    @Override
    public void onError(final int code, final String message) {
        if (handler != null) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    onAdFailed(code, message);
                }
            });
        }
    }

    @Override
    public void onFullScreenVideoAdLoad(TTFullScreenVideoAd ttFullScreenVideoAd) {
        if (ttFullScreenVideoAd == null) {
            if (handler != null) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        onAdFailed(ADSuyiErrorConfig.AD_FAILED_AD_IS_EMPTY, ADSuyiErrorConfig.MSG_AD_FAILED_AD_IS_EMPTY);
                    }
                });
            }
            return;
        }
        fullScreenVodAdInfo = new FullScreenVodAdInfo(getPlatformPosId());
        fullScreenVodAdInfo.setAdapterAdInfo(ttFullScreenVideoAd);
        fullScreenVodAdInfo.setAdListener(getAdListener());
        if (handler != null) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    if (getAdListener() != null) {
                        getAdListener().onAdReceive(fullScreenVodAdInfo);
                    }
                }
            });
        }
    }

    @Override
    public void onFullScreenVideoCached() {
    }

    @Override
    public void onFullScreenVideoCached(TTFullScreenVideoAd ttFullScreenVideoAd) {
        if (handler != null) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    if (getAdListener() != null && fullScreenVodAdInfo != null) {
                        getAdListener().onVideoCache(fullScreenVodAdInfo);
                    }
                }
            });
        }
    }

    /**
     * 广告是否资源
     */
    @Override
    public void release() {
        super.release();
        if (fullScreenVodAdInfo != null) {
            fullScreenVodAdInfo.release();
            fullScreenVodAdInfo = null;
        }
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
            handler = null;
        }
    }
}
